package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.search.domian.DestinationEs;
import cn.wolfcode.wolf2w.search.domian.StrategyEs;
import cn.wolfcode.wolf2w.search.domian.TravelEs;
import cn.wolfcode.wolf2w.search.domian.UserInfoEs;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.service.ISearchService;
import cn.wolfcode.wolf2w.search.vo.ParamMap;
import cn.wolfcode.wolf2w.search.vo.SearchParamVO;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.List;

@RestController
public class SearchController {
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ISearchService searchService;

    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws Exception {
        // get请求编码解决
        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(decode);
        switch (qo.getType()) {
            // 目的地
            case SearchQueryObject.TYPE_DEST:
                return this.searchDest(qo);
            // 攻略
            case SearchQueryObject.TYPE_STRATEGY:
                return this.searchStrategy(qo);
            // 游记
            case SearchQueryObject.TYPE_TRAVEL:
                return this.searchTravel(qo);
            // 用户
            case SearchQueryObject.TYPE_USER:
                return this.searchUser(qo);
            // 全部
            default:
                return this.searchAll(qo);
        }
    }

    // 攻略
    private Object searchStrategy(SearchQueryObject qo) {
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");
        return JsonResult.success(new ParamMap().put("page", page).put("qo", qo));
    }

    // 游记
    private Object searchTravel(SearchQueryObject qo) {
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");
        return JsonResult.success(new ParamMap().put("page", page).put("qo", qo));
    }

    // 用户
    private Object searchUser(SearchQueryObject qo) {
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");
        return JsonResult.success(new ParamMap().put("page", page).put("qo", qo));
    }

    // 目的地
    private Object searchDest(SearchQueryObject qo) {
        //前端需要的数据
        // result(total  strategys  travels  users)
        // dest
        // qo
        SearchParamVO vo = new SearchParamVO();
        // 查询目的地
        Destination dest = destinationService.findByName(qo.getKeyword());
        if (dest != null) {
            // 攻略
            List<Strategy> strategies = strategyService.findByDestId(dest.getId());
            vo.setStrategys(strategies);
            // 游记
            List<Travel> travels = travelService.findByDestId(dest.getId());
            vo.setTravels(travels);
            // 用户
            List<UserInfo> user = userInfoService.findByCity(dest.getName());
            vo.setUsers(user);
            // 总数
            vo.setTotal(strategies.size() + travels.size() + user.size() + 0L);
        }
        return JsonResult.success(ParamMap.newInstance().put("result", vo).put("dest",dest).put("qo",qo));
    }

    // 全部
    private Object searchAll(SearchQueryObject qo) {
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo,
                "title", "subTitle", "summary");

        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME, Travel.class, qo,
                "title", "summary");

        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo,
                "city", "info");

        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo,
                "name", "info");

        SearchParamVO vo = new SearchParamVO();
        vo.setDests(ds.getContent());
        vo.setStrategys(sts.getContent());
        vo.setTravels(ts.getContent());
        vo.setUsers(us.getContent());
        vo.setTotal(sts.getTotalElements() + ts.getNumberOfElements() + us.getTotalElements() + ds.getTotalElements());
        return JsonResult.success(new ParamMap().put("result", vo).put("qo", qo));
    }
}
